package org.lq.er.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.lq.er.dao.Exam_positionDao;
import org.lq.er.entity.Admin;
import org.lq.er.entity.Exam_position;
import org.lq.er.util.Instantiation;
import org.lq.er.util.JDBCUtil;

/**
 * 考场数据访问实现类
 * @author 张鹤山
 *
 */
public class Exam_positionDaoImpl implements Exam_positionDao, Instantiation<Exam_position> {

	/**
	 * 添加考场
	 */
	@Override
	public int save(Exam_position t) {
		String sql = "insert into exam_position values(?,?,?)";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, t.getId(),t.getPosition(),t.getMax_count());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 修改考场
	 */
	@Override
	public int update(Exam_position t) {
		String sql = "update exam_position set position=?,max_count=? where id=?";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, t.getPosition(),t.getMax_count(),t.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 通过编号删除考场
	 */
	@Override
	public int delete(int id) {
		String sql = "delete from exam_position where id=?";
		int flag = 0;
		try {
			flag = JDBCUtil.executeUpdate(sql, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 查询所有考场
	 */
	@Override
	public List<Exam_position> findAll() {
		List<Exam_position> list = new ArrayList<>();
		String sql = "select * from exam_position";
		try {
			list = JDBCUtil.executeQuery(sql, this);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 根据编号查询
	 */
	@Override
	public Exam_position getById(int id) {
		List<Exam_position> list = new ArrayList<>();
		String sql = "select * from exam_position where id=?";
		try {
			list = JDBCUtil.executeQuery(sql, this, id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (list.size()>0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 实例化考场
	 */
	@Override
	public Exam_position instance(ResultSet rs) {
		Exam_position exam_position = new Exam_position();
		try {
			exam_position.setId(rs.getInt("id"));
			exam_position.setPosition(rs.getString("position"));
			exam_position.setMax_count(rs.getInt("max_count"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return exam_position;
	}

}
